package com.scau.yangsuiyu.estore.service;

import com.alibaba.fastjson.JSONObject;
import com.scau.yangsuiyu.estore.dababases.dao.BuyerDao;
import com.scau.yangsuiyu.estore.dababases.dao.SellerDao;
import com.scau.yangsuiyu.estore.dababases.entity.Buyer;
import com.scau.yangsuiyu.estore.dababases.entity.Seller;
import com.scau.yangsuiyu.estore.dababases.entity.User;

import java.sql.Timestamp;

/**
 * @author suiyu_yang
 * @description 实现注册的业务逻辑
 * @date 2019-05-20 10:27
 * @email suiyu_yang@163.com
 */
public class RegisterService {
    private User user;

    public RegisterService(User user) {
        this.user = user;
    }

    public JSONObject register() {

        //设置默认值
        setDefaultValue();
        if (user instanceof Buyer) {
            return buyerRegister();
        } else if (user instanceof Seller) {
            return sellerRegister();
        }


        JSONObject resultJson = new JSONObject();
        resultJson.put("status", "failed");
        resultJson.put("result", "注册参数错误！");

        return resultJson;
    }

    private JSONObject sellerRegister() {
        JSONObject resultJson = new JSONObject();
        SellerDao sellerDao = new SellerDao();

        if (ifSellerUsernameExist(user.getUsername())) { //存在就报错
            resultJson.put("status", "failed");
            resultJson.put("result", "会员名已存在！");
        } else { //不存在，可以可以注册
            sellerDao.insert((Seller) user);

            //更新昵称
            updateNickName(user);

            resultJson.put("status", "success");
            resultJson.put("user", user);
        }

        return resultJson;

    }

    private JSONObject buyerRegister() {
        JSONObject resultJson = new JSONObject();
        BuyerDao buyerDao = new BuyerDao();

        if (ifBuyerUsernameExist(user.getUsername())) { //存在用户名
            resultJson.put("status", "failed");
            resultJson.put("result", "会员名已存在！");
        } else { //不存在
            buyerDao.insert((Buyer) user);

            //更新昵称
            updateNickName(user);

            resultJson.put("status", "success");
            resultJson.put("user", user);
        }

        return resultJson;
    }

    /**
     * 验证buyer手机号码是否在数据库中存在
     *
     * @param phone
     * @return false表示不存在，true表示存在
     */
    public static boolean ifBuyerPhoneExist(String phone) {
        BuyerDao buyerDao = new BuyerDao();

        if (buyerDao.getBuyerByPhone(phone) == null) {
            return false;
        }
        return true;

    }

    /**
     * 验证seller手机号码是否在数据库中存在
     *
     * @param phone
     * @return false表示不存在，true表示存在
     */
    public static boolean ifSellerPhoneExist(String phone) {
        SellerDao sellerDao = new SellerDao();

        if (sellerDao.getSellerByPhone(phone) == null) {
            return false;
        }

        return true;
    }

    /**
     * 验证buyer的username是否存在数据库中
     *
     * @param username
     * @return false表示不存在，true表示存在
     */
    private boolean ifBuyerUsernameExist(String username) {
        BuyerDao buyerDao = new BuyerDao();

        if (buyerDao.getBuyerByUsername(username) == null) {
            return false;
        }
        return true;
    }

    /**
     * 验证seller的username是否存在数据库中
     *
     * @param username
     * @return false表示不存在，true表示存在
     */
    private boolean ifSellerUsernameExist(String username) {
        SellerDao sellerDao = new SellerDao();
        if (sellerDao.getSellerByUsername(username) == null) {
            return false;
        }
        return true;
    }

    /**
     * 统一设置默认值
     */
    private void setDefaultValue() {
        Timestamp currentTimeStamp = new Timestamp(System.currentTimeMillis());
        user.setCreateTime(currentTimeStamp);
        user.setProfilePhoto("/Users/suisui/Documents/Idea/EstoreJavaFX/src/main/resources/image/default_photo_male.png");
    }

    /**
     * 插入的同时更新昵称
     *
     * @param user
     */
    private void updateNickName(User user) {
        SellerDao sellerDao = new SellerDao();
        BuyerDao buyerDao = new BuyerDao();

        if (user instanceof Seller) {
            user = sellerDao.getSellerByPhone(user.getPhone());
            user.setNickname("ss_seller_" + user.getId());

            sellerDao.update((Seller) user);
        } else {
            user = buyerDao.getBuyerByPhone(user.getPhone());
            user.setNickname("ss_buyer_" + user.getId());

            buyerDao.update((Buyer) user);

        }
    }

}
